package com.zxw.producer;

import com.zxw.queue.DisruptorQueue;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author by zhuxiansheng
 * @description 生产者线程
 * @date 2020/7/10 11:16
 */
public class ProducerThread implements Runnable{
    private String name;
    private DisruptorQueue disruptorQueue;
    private volatile boolean flag = true;
    private static AtomicInteger count = new AtomicInteger();

    public ProducerThread(String name, DisruptorQueue disruptorQueue) {
        this.name = name;
        this.disruptorQueue = disruptorQueue;
    }


    @Override
    public void run() {
        System.out.println(LocalDateTime.now().toString() + name + "线程启动");
        while (flag) {
            String data = count.incrementAndGet() + "";
            disruptorQueue.add(data);
            System.out.println(LocalDateTime.now().toString() + " 数据 " + data + " 入队");
        }
        System.out.println(LocalDateTime.now().toString() + name + "线程退出");

    }

    public void stopThread() {
        this.flag = false;
    }
}
